//
//  ProductMyComment.m
//  FloatClouds
//
//  Created by 黄威 on 16/9/12.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "ProductMyComment.h"
#import "BaseButton.h"
#import "BaseLabel.h"
#import "HWLNetworkTool.h"
#import "CenterViewController.h"
#import "NSString+HVV.h"
@interface ProductMyComment ()
@property (nonatomic, retain) BaseLabel *labelOfLandPrice;
@property (nonatomic, retain) BaseLabel *labelOflanPricePerPacking;
@property (nonatomic, retain) UITextField *textFieleOfPrice;
@property (nonatomic, retain) UITextView *textFieldOfComment;
@property (nonatomic, retain) BaseButton *button;
@property (nonatomic, copy) NSString *body;
@end
@implementation ProductMyComment

- (void)viewDidLoad{

    self.labelOfLandPrice = [[BaseLabel alloc]init];
    self.labelOfLandPrice.font = [UIFont fontWithName:@"Helvetica" size:17];
    [self.view addSubview:self.labelOfLandPrice];
    self.labelOfLandPrice.frame = CGRectMake(self.view.bounds.size.width *0.05 ,74, self.view.bounds.size.width *0.25, self.view.bounds.size.width *0.1);
    self.labelOfLandPrice.text = @"当地售价:";
    [self.view addSubview:self.labelOfLandPrice];
    
    
    self.textFieleOfPrice = [[UITextField alloc]init];
    self.textFieleOfPrice.frame = CGRectMake(self.view.bounds.size.width *0.3,74, self.view.bounds.size.width *0.15, self.view.bounds.size.height *0.05);
    self.textFieleOfPrice.borderStyle =  UITextBorderStyleRoundedRect;
    [self.view addSubview:self.textFieleOfPrice];
    
    

    
    
    self.labelOflanPricePerPacking = [[BaseLabel alloc]init];
   self.labelOflanPricePerPacking.font = [UIFont fontWithName:@"Helvetica" size:17];
    [self.view addSubview:self.labelOflanPricePerPacking];
   self.labelOflanPricePerPacking.frame = CGRectMake(self.view.bounds.size.width *0.45 , 74, self.view.bounds.size.width *0.4, self.view.bounds.size.width *0.1);
   self.labelOflanPricePerPacking.text = @"元/包(选填)";
    [self.view addSubview:self.labelOflanPricePerPacking];
    
    
    
    self.textFieldOfComment = [[UITextView alloc]init];
    self.textFieldOfComment.frame = CGRectMake(self.view.bounds.size.width *0.05 , self.view.bounds.size.width *0.2 + 74, self.view.bounds.size.width *0.9, self.view.bounds.size.width *0.6);
    self.textFieldOfComment.backgroundColor = [UIColor colorWithRed:247 / 255.0 green:247/255.0 blue:247/255.0 alpha:1];
    self.textFieldOfComment.font = self.labelOfLandPrice.font = [UIFont fontWithName:@"Helvetica" size:17];
    [self.textFieldOfComment.layer setMasksToBounds:YES];
    [self.textFieldOfComment.layer setCornerRadius:5.0];
    
    [self.view addSubview:self.textFieldOfComment];
    
    
    
    
    
    self.button = [[BaseButton alloc]initWithFrame:CGRectMake(self.view.bounds.size.width *0.2, self.view.bounds.size.width *0.8 + 104, self.view.bounds.size.width *0.6 ,self.view.bounds.size.width * 0.1)];
    [self.button.layer setMasksToBounds:YES];
    [self.button.layer setCornerRadius:5.0];
    self.button.backgroundColor = [UIColor colorWithRed:18.0 / 255 green:86.0 / 255 blue:138.0 / 255 alpha:1];
    [self.button setTitleColor:[ UIColor whiteColor] forState:UIControlStateNormal];
    [self.button setTitle:@"发表" forState:UIControlStateNormal];
    [self.view addSubview:self.button];
    [self.button addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];


}

- (void)submit{

    NSDictionary *dicOfHeader = @{@"Content-Type":@"application/x-www-form-urlencoded"};
    
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *pathCookie = [[path lastObject]stringByAppendingPathComponent:@"cookie.TXT"];
    
    NSString *pathuuid = [[path lastObject]stringByAppendingPathComponent:@"uid.TXT"];
    
    NSString *cookie = [NSString stringWithContentsOfFile:pathCookie encoding:NSUTF8StringEncoding error:nil];
    
    NSString *uid = [NSString stringWithContentsOfFile:pathuuid encoding:NSUTF8StringEncoding error:nil];
    
    
    NSString *content = [self.textFieldOfComment.text encodeToPercentEscapeString:self.textFieldOfComment.text];
    
    if (self.textFieleOfPrice.text.length > 0) {
        
        self.body = [[[[[[[[[@"ID="stringByAppendingString:self.productID]stringByAppendingString:@"&content="]stringByAppendingString:content]stringByAppendingString:@"&cookie="]stringByAppendingString:cookie]stringByAppendingString:@"&uid="]stringByAppendingString:uid]stringByAppendingString:@"&price="]stringByAppendingString:self.textFieleOfPrice.text];
    }else{
    
        self.body = [[[[[[[@"ID="stringByAppendingString:self.productID]stringByAppendingString:@"&content="]stringByAppendingString:content]stringByAppendingString:@"&cookie="]stringByAppendingString:cookie]stringByAppendingString:@"&uid="]stringByAppendingString:uid];
    
    }
    
   
    
    
    [HWLNetworkTool POST:@"http://app.yanyue.cn/product/addcomment" andBody:self.body andBodyStyle:HWLBodyString andHeader:dicOfHeader andResponse:HWLJSON andSuccessBlock:^(id result) {
        
        
        NSInteger status = [result[@"status"] integerValue];
        
        NSString *msg = result[@"msg"];
        
        if (status == 0) {
            
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:msg message:nil preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            
            [self presentViewController:alert animated:YES completion:nil];
            
        }
                
        if (status == 1) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"评论成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [self.navigationController popViewControllerAnimated:YES];
            }]];
            
            [self presentViewController:alert animated:YES completion:nil];
            
        }
        
  
    } andFailureBlock:^(NSError *error) {
        
    }];
    


}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{


    [self.textFieleOfPrice resignFirstResponder];
    [self.textFieldOfComment resignFirstResponder];

}


@end
